package com.example.aigc_education.controller;

import com.example.aigc_education.anno.RequestKeyParam;
import com.example.aigc_education.anno.RequestLock;
import com.example.aigc_education.common.BusinessException;
import com.example.aigc_education.domain.request.AddQuizzesRequest;
import com.example.aigc_education.domain.request.SaveOrSubmitQuizRequest;
import com.example.aigc_education.domain.vo.BriefQuizVO;
import com.example.aigc_education.domain.vo.EntireQuizVO;
import com.example.aigc_education.domain.vo.IdVO;
import com.example.aigc_education.service.PaperService;
import com.example.aigc_education.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/quiz")
public class QuizController {
    @Resource
    private PaperService paperService;

    /**
     * 展示该主题下的测验集合列表
     * @param themeId 主题id
     * @return 简单测验信息集合列表
     */
    @GetMapping("/{themeId}/getBriefList")
    public ResultUtil showAllQuizList(@PathVariable @NotNull(message = "主题id不能为空")Long themeId){
        List<BriefQuizVO> briefQuizListVOS =  paperService.showAllQuiz(themeId);

        return ResultUtil.success(briefQuizListVOS);
    }


    /**
     * 通过测验id 展示一个完整的测验
     * @param quizId 测验id
     * @return 一个完整的测验
     */
    @GetMapping("/{quizId}/getEntireOne")
    public ResultUtil getEntireQuizList( @PathVariable @NotNull(message = "测验id不能为空")Long quizId){
        EntireQuizVO entireQuizVO =  paperService.getEntireQuiz(quizId);
        return ResultUtil.success(entireQuizVO);
    }

    @RequestLock(prefix = "saveQuiz:",expire = 3)
    @PutMapping("/save")
    public ResultUtil saveQuiz(@RequestKeyParam @RequestBody @Validated SaveOrSubmitQuizRequest saveOrSubmitQuizRequest){
        boolean success = paperService.saveQuiz(saveOrSubmitQuizRequest);
        if (!success) {
            throw new BusinessException("操作失败");
        }
        return ResultUtil.success("操作成功");
    }


    @RequestLock(prefix = "redoQuiz:",expire = 3)
    @PutMapping("/{quizId}/redo")
    public ResultUtil redoQuiz(@RequestKeyParam @PathVariable @NotNull(message = "测验id不能为空") Long quizId){
        boolean success = paperService.redoQuiz(quizId);
        if (!success) {
            throw new BusinessException("操作失败");
        }
        return ResultUtil.success("操作成功");
    }

    @RequestLock(prefix = "submitQuiz:",expire = 3)
    @PutMapping("/submit")
    public ResultUtil submitQuiz(@RequestKeyParam @RequestBody @Validated SaveOrSubmitQuizRequest saveOrSubmitQuizRequest){
        boolean success = paperService.submitQuiz(saveOrSubmitQuizRequest);
        if (!success) {
            throw new BusinessException("操作失败");
        }
        return ResultUtil.success("操作成功");
    }



    /**
     * 删除测验
     * @param quizId 测验id
     * @return 返回删除测验结果
     */
    @RequestLock(prefix = "deleteQuiz:",expire = 3)
    @PutMapping("/{quizId}/delete")
    public ResultUtil deleteQuiz(@RequestKeyParam @PathVariable @NotNull(message = "测验id不能为空") Long quizId){
        boolean removed = paperService.deleteQuiz(quizId);
        if (!removed) {
            throw new BusinessException("删除失败");
        }
        return ResultUtil.success("删除成功");
    }

    @RequestLock(prefix = "batchSaveQuiz:",expire = 3)
    @PostMapping("/batch/save")
    public ResultUtil batchSaveQuiz(@RequestKeyParam @Validated @RequestBody AddQuizzesRequest request){
        Long paperId = paperService.batchSaveQuiz(request);
        if (paperId == null) {
            throw new BusinessException("保存失败");
        }
        return ResultUtil.success("保存成功",new IdVO(paperId));
    }
}
